package com.ruoyi.manage.controller;

import com.alipay.api.AlipayApiException;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.manage.service.IAliPayService;
import com.ruoyi.manage.service.IWxChatPayService;
import com.ruoyi.manage.utils.AliPayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.Map;

/**
 * @author : 赵志冲
 * @date : 2024-07-02 20:31
 * @description :
 **/
@RestController
@RequestMapping("/manage/pay")
public class PayController {
    @Autowired
    IWxChatPayService wxChatPayService;

    @Autowired
    IAliPayService aliPayService;

    @PostMapping("wxPay/{orderId}/{amount}")
    public R<Map<String,Object>> pay(@PathVariable String orderId,
                                     @PathVariable Integer amount){
        return wxChatPayService.wxChatPay(orderId,amount);
    }

    @GetMapping("QueryWxOrder/{orderId}")
    public R<String> orderQuery(@PathVariable Long orderId){
        return wxChatPayService.queryWxOrder(orderId);
    }


    @GetMapping ("aliPay/{orderId}/{amount}")
    public String aliPay(@PathVariable("orderId") String orderId, @PathVariable("amount") Integer amount){
        return aliPayService.aliPay(orderId,amount);
    }
    @PostMapping("callback")
    public String callback(String out_trade_no, HttpServletRequest request) throws AlipayApiException, UnsupportedEncodingException {
        boolean b = AliPayUtils.callBack(request);
        if (b){
            System.out.println(out_trade_no);
            return "支付成功";
        } else {
            return "支付失败";
        }
    }
}
